home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d985.lha / NewIFF / NewIFF39.lha / newiff39 / other / clipftxt.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  6KB  |  248 lines

  1. ;/* clipftxt.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -j73 clipftxt.c
  3. Blink FROM LIB:c.o,clipftxt.o TO clipftxt LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. *
  7. * clipftxt.c:    Writes ASCII text to clipboard unit as FTXT
  8. *        (All clipboard data must be IFF)
  9. *
  10. * Usage: clipftxt unitnumber
  11. *
  12. * To convert to an example of reading only, comment out #define WRITEREAD
  13. */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/memory.h>
  17. #include <libraries/dos.h>
  18. #include <libraries/iffparse.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/iffparse_protos.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. #ifdef LATTICE
  28. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  29. int chkabort(void) { return(0); }  /* really */
  30. #endif
  31.  
  32. /* Causes example to write FTXT first, then read it back
  33.  * Comment out to create a reader only
  34.  */
  35. #define WRITEREAD
  36.  
  37.  
  38. #define MINARGS 2
  39.  
  40. /* 2.0 Version string for c:Version to find */
  41. UBYTE vers[] = "\0$VER: clipftxt 37.2";
  42.  
  43. UBYTE usage[] = "Usage: clipftxt unitnumber (use zero for primary unit)";
  44.  
  45. /*
  46.  * Text error messages for possible IFFERR_#? returns from various
  47.  * IFF routines.  To get the index into this array, take your IFFERR code,
  48.  * negate it, and subtract one.
  49.  *  idx = -error - 1;
  50.  */
  51. char    *errormsgs[] = {
  52.     "End of file (not an error).",
  53.     "End of context (not an error).",
  54.     "No lexical scope.",
  55.     "Insufficient memory.",
  56.     "Stream read error.",
  57.     "Stream write error.",
  58.     "Stream seek error.",
  59.     "File is corrupt.",
  60.     "IFF syntax error.",
  61.     "Not an IFF file.",
  62.     "Required call-back hook missing.",
  63.     "Return to client.  You should never see this."
  64. };
  65.  
  66. #define RBUFSZ 512
  67.  
  68. #define  ID_FTXT    MAKE_ID('F','T','X','T')
  69. #define  ID_CHRS    MAKE_ID('C','H','R','S')
  70.  
  71. struct Library *IFFParseBase;
  72.  
  73. UBYTE mytext[]="This FTXT written to clipboard by clipftxt example.\n";
  74.  
  75. void main(int argc, char **argv)
  76. {
  77.     struct IFFHandle    *iff = NULL;
  78.     struct ContextNode  *cn;
  79.     long        error=0, unitnumber=0, rlen;
  80.     int textlen;
  81.     UBYTE readbuf[RBUFSZ];
  82.  
  83.         /* if not enough args or '?', print usage */
  84.         if(((argc)&&(argc<MINARGS))||(argv[argc-1][0]=='?'))
  85.         {
  86.             printf("%s\n",usage);
  87.             exit(RETURN_WARN);
  88.             }
  89.  
  90.     unitnumber = atoi(argv[1]);
  91.     
  92.     if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L)))
  93.         {
  94.         puts("Can't open iff parsing library.");
  95.         goto bye;
  96.         }
  97.  
  98.     /*
  99.      * Allocate IFF_File structure.
  100.      */
  101.     if (!(iff = AllocIFF ()))
  102.         {
  103.         puts ("AllocIFF() failed.");
  104.         goto bye;
  105.         }
  106.  
  107.     /*
  108.      * Set up IFF_File for Clipboard I/O.
  109.      */
  110.     if (!(iff->iff_Stream = (ULONG) OpenClipboard (unitnumber)))
  111.         {
  112.         puts ("Clipboard open failed.");
  113.         goto bye;
  114.         }
  115.     else printf("Opened clipboard unit %ld\n",unitnumber);
  116.  
  117.     InitIFFasClip (iff);
  118.  
  119. #ifdef WRITEREAD
  120.  
  121.     /*
  122.      * Start the IFF transaction.
  123.      */
  124.     if (error = OpenIFF (iff, IFFF_WRITE))
  125.         {
  126.         puts ("OpenIFF for write failed.");
  127.         goto bye;
  128.         }
  129.  
  130.     /*
  131.      * Write our text to the clipboard as CHRS chunk in FORM FTXT
  132.      *
  133.      * First, write the FORM ID (FTXT)
  134.      */
  135.         if(!(error=PushChunk(iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)))
  136.         {
  137.         /* Now the CHRS chunk ID followed by the chunk data
  138.          * We'll just write one CHRS chunk.
  139.          * You could write more chunks.
  140.          */
  141.             if(!(error=PushChunk(iff, 0, ID_CHRS, IFFSIZE_UNKNOWN)))
  142.             {
  143.             /* Now the actual data (the text) */
  144.             textlen = strlen(mytext);
  145.             if(WriteChunkBytes(iff, mytext, textlen) != textlen)
  146.                 {
  147.                 puts("Error writing CHRS data.");
  148.                 error = IFFERR_WRITE;
  149.                 }
  150.             }
  151.         if(!error) error = PopChunk(iff);
  152.         }
  153.     if(!error) error = PopChunk(iff);
  154.  
  155.  
  156.     if(error)
  157.         {
  158.         printf ("IFF write failed, error %ld: %s\n",
  159.             error, errormsgs[-error - 1]);
  160.         goto bye;
  161.         }
  162.     else printf("Wrote text to clipboard as FTXT\n");
  163.  
  164.     /*
  165.      * Now let's close it, then read it back
  166.      * First close the write handle, then close the clipboard
  167.      */
  168.     CloseIFF(iff);
  169.     if (iff->iff_Stream) CloseClipboard ((struct ClipboardHandle *)
  170.                         iff->iff_Stream);
  171.  
  172.     if (!(iff->iff_Stream = (ULONG) OpenClipboard (unitnumber)))
  173.         {
  174.         puts ("Reopen of Clipboard failed.");
  175.         goto bye;
  176.         }
  177.     else printf("Reopened clipboard unit %ld\n",unitnumber);
  178.  
  179. #endif /* WRITEREAD */
  180.  
  181.     if (error = OpenIFF (iff, IFFF_READ))
  182.         {
  183.         puts ("OpenIFF for read failed.");
  184.         goto bye;
  185.         }
  186.  
  187.     /* Tell iffparse we want to stop on FTXT CHRS chunks */
  188.     if (error = StopChunk(iff, ID_FTXT, ID_CHRS))
  189.         {
  190.         puts ("StopChunk failed.");
  191.         goto bye;
  192.         }
  193.  
  194.     /* Find all of the FTXT CHRS chunks */
  195.     while(1)
  196.         {
  197.         error = ParseIFF(iff,IFFPARSE_SCAN);
  198.         if(error == IFFERR_EOC) continue;    /* enter next context */
  199.         else if(error) break;
  200.  
  201.         /* We only asked to stop at FTXT CHRS chunks
  202.          * If no error we've hit a stop chunk
  203.          * Read the CHRS chunk data
  204.          */
  205.         cn = CurrentChunk(iff);
  206.  
  207.         if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CHRS))
  208.             {
  209.             printf("CHRS chunk contains:\n");
  210.             while((rlen = ReadChunkBytes(iff,readbuf,RBUFSZ)) > 0)
  211.                 {
  212.                 Write(Output(),readbuf,rlen);
  213.                 }
  214.             if(rlen < 0)    error = rlen;    
  215.             }
  216.         }
  217.  
  218.     if((error)&&(error != IFFERR_EOF))
  219.         {
  220.         printf ("IFF read failed, error %ld: %s\n",
  221.             error, errormsgs[-error - 1]);
  222.         }
  223.  
  224. bye:
  225.     if (iff) {
  226.         /*
  227.          * Terminate the IFF transaction with the stream.  Free
  228.          * all associated structures.
  229.          */
  230.         CloseIFF (iff);
  231.  
  232.         /*
  233.          * Close the clipboard stream
  234.          */
  235.         if (iff->iff_Stream)
  236.                 CloseClipboard ((struct ClipboardHandle *)
  237.                         iff->iff_Stream);
  238.         /*
  239.          * Free the IFF_File structure itself.
  240.          */
  241.         FreeIFF (iff);
  242.         }
  243.     if (IFFParseBase)    CloseLibrary (IFFParseBase);
  244.  
  245.     exit (RETURN_OK);
  246. }
  247.  
  248.